package com.codeblue.tools.codeGenerator;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;


/**
 * 注解生成器 生成 带有FiledComment 注解POJO
 * @author jiulong.hu
 * 
 */
public class AnnotateGenerator  extends AbstractGenerator{
	
	
	
	
	public static void main(String[] args) {
		AnnotateGenerator g = new AnnotateGenerator();
		g.genPojoWithAnnotaion("f:\\1.txt","xfdf");
	}

	
	
	
	
	
	
	public void genPojoWithAnnotaion(String sourceFile,String pojoName) {
		
		
		Map<String,Object> root = new HashMap<String,Object>();
		Map<String,Object> pojo = new HashMap<String,Object>();
		List<Map<String,Object>>  fieldlist = new ArrayList<Map<String,Object>>();
		
		root.put("pojo", pojo);
		pojo.put("className", pojoName);
		pojo.put("fieldlist", fieldlist);
		int count = 0;
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(sourceFile), "GBK"));
			String line = null;
			while ((line = reader.readLine()) != null) {
				count ++ ;
				if(count==1) continue ;
				String tokens[] = line.split("\\s+");
				Map<String,Object> field = new HashMap<String,Object>();
				field.put("name", tokens[0]);
				field.put("code", tokens[1].toLowerCase());
				field.put("type", adapteType(tokens[2]));
				field.put("primary", tokens[5]);
				fieldlist.add(field);
			}
			reader.close();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		super.genFile("pojo.ftl", "E:\\workspace\\wsq\\src\\com\\wsq\\db\\entity\\"+pojoName+".java", root);
		
	}
	
	
	public String adapteType(String str){
		if(StringUtils.startsWith(str, "bigint")){
			return "Long" ;
		}
		if(StringUtils.startsWith(str, "int")){
			return "Integer" ;
		}
		if(StringUtils.startsWith(str, "varchar")){
			return "String" ;
		}
		if(StringUtils.startsWith(str, "numeric")){
			return "Integer" ;
		}
		if(StringUtils.startsWith(str, "char")){
			return "String" ;
		}
		if(StringUtils.startsWith(str, "decimal")){
			return "Double" ;
		}
		if(StringUtils.startsWith(str, "datetime")){
			return "String" ;
		}
		if(StringUtils.startsWith(str, "date")){
			return "String" ;
		}
		return str;
	}

}
